function AccountView(div, handler, config) {

	var self = this;
	this.account = null;
	var c = {
		width: 400,
		height: 190,
		dialogTitle: 'Счет',
		curManager: null
	};
	for(var property in config) {
		c[property] = config[property];
	}
	var curCombo = new ComboView({
		width: 60,
		requestEvent: 'currencies-request',
		updateEvent: 'currencies-loaded'
	});
	$.subscribe('currencies-loaded', function(event, data) {
		curCombo.updateItems(data.items);
	});

	this.init = function() {
		var html = '<div id="' + div + 'Title"></div>' +
			'<div id="' + div + 'Content"><table class="table-layout" align="center">' +
				'<tr><td align="right">Наименование:</td><td><input id="' + div + 'Name" type="text" /></td></tr>' +
				'<tr><td align="right">Дата создания:</td><td><div id="' + div + 'Date"></div></td></tr>' +
				'<tr><td colspan="2"><div id="' + div + 'Loan">контрагент</div></td></tr>' +
				'<tr><td colspan="2"><div id="' + div + 'InitialBalances">' +
					'<table align="right">' +
						'<tr><td colspan="3"><b>Начальный баланс:</b></td>' +
						'<tr><td><div id="' + div + 'InitBalanceAddSum" type="text"></div></td>' +
							'<td><div id="' + div + 'InitBalanceAddCurrency"></div></td>' +
							'<td><input id="' + div + 'InitBalanceAddBtn" type="button" value="+" /></td></tr>' +
					'</table>' +
					'<br><div id="' + div + 'InitBalancesTable"></div>' +
				'</div></td></tr>' +
				'<tr><td colspan="2" align="center"><input id="' + div + 'SubmitBtn" type="button" value="OK" />' +
					'&nbsp;&nbsp;<input id="' + div + 'CancelBtn" type="button" value="Отмена" /></td></tr>' +
			'</table></div>';
		el(div).html(html);

		el(div + 'Name').jqxInput({ width: 120, height: 22 });
		el(div + 'Date').jqxDateTimeInput({ width: 120, height: 25, formatString: 'dd.MM.yyyy' });
		el(div + 'Loan').jqxCheckBox({ checked: false }).on('change', function(event) {
			self.setAccountOwn(!event.args.checked);
		});

//		el(div + 'InitBalancesTable').jqxPanel({ width: 240, height: 95 });
		el(div + 'InitBalanceAddSum').jqxNumberInput({ width: 100, height: 22, inputMode: 'simple', spinButtons: true });
		curCombo.render(div + 'InitBalanceAddCurrency');
		el(div + 'InitBalanceAddBtn').jqxButton({ width: 24, height: 24 }).on('click', function() {
			var sum = el(div + 'InitBalanceAddSum').jqxNumberInput('getDecimal');
			var currency = curCombo.value;
			if(currency != null && sum > 0) {
				self.addBalance(sum, currency.name);
				curCombo.exclude(currency.name);
			}
		});

		el(div + 'SubmitBtn').jqxButton({ width: 70 }).on('click', function() {
			var name = el(div + 'Name').val();
			if(isNotEmpty(name)) {
				self.close();
				if(handler != null) {
					handler(self.account);
				}
			}
		});
		el(div + 'CancelBtn').jqxButton({ width: 70 }).on('click', function() {
			self.close();
		});
		el(div).jqxWindow({ resizable: false, draggable: true, isModal: true, autoOpen: false,
			width: c.width, height: c.height });
	};

	this.setAccountOwn = function(own) {
		this.account.own = own;
		this.updateInitialBalances();
	};

	this.addBalance = function(sum, currency) {
		if(this.account.addInitialBalance(sum, currency)) {
			this.updateInitialBalances();
		}
	};

	this.removeBalance = function(currency) {
		if(this.account.removeInitialBalance(currency)) {
			this.updateInitialBalances();
		}
	};

	this.updateForm = function() {
		el(div + 'Name').val(this.account.name);
		el(div + 'Loan').jqxCheckBox({ checked: !this.account.own });
		this.updateInitialBalances();
	};

	this.updateInitialBalances = function() {
		if(this.account.own) {
			var html = '<table align="right">';
			$.each(this.account.initialBalances, function(index, balance) {
				curCombo.exclude(balance.currency);
				html += '<tr><td align="right">' + formatSum(balance.sum) + '</td><td>' + balance.currency + '</td>' +
					'<td><input id="' + div + 'InitBalanceRemoveBtn' + index + '" type="button" value="X" /></td></tr>';
			});
			html += '</table>';
			el(div + 'InitBalancesTable').html(html);
			$.each(this.account.initialBalances, function(index, balance) {
				el(div + 'InitBalanceRemoveBtn' + index).jqxButton({ width: 24, height: 24 }).on('click', function() {
					self.removeBalance(balance.currency);
					curCombo.disexclude(balance.currency);
				});
			});
			el(div + 'InitBalancesTable').jqxPanel({ width: 240, height: 95 });
			el(div + 'InitialBalances').css('display', 'block');
			el(div).jqxWindow({ height: c.height + 150 });
		} else {
			el(div + 'InitialBalances').css('display', 'none');
			el(div).jqxWindow({ height: c.height });
		}
	};

	this.show = function(account) {
		var title;
		if(account != null) {
			title = 'Редактирование счета';
			this.account = account.clone();
		} else {
			title = 'Новый счет';
			this.account = new Account(null, '', true);
		}
		el(div + 'Title').html(title);
		this.updateForm();
		el(div).jqxWindow('open');
	};

	this.close = function() {
		el(div).jqxWindow('close');
	};
}